﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BEGDC.PreContract.BLL;
using System.Text;

namespace PreContractWeb.Report
{
    public partial class ExportToExcel : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string rptType = "";
            string optType = "";
            string rptID = "";


            if (Request.QueryString["rptType"] != null)
            {
                rptType = Request.QueryString["rptType"];
            }
            if (Request.QueryString["optType"] != null)
            {
                optType = Request.QueryString["optType"];
            }
             if (Request.QueryString["rptID"] != null)
            {
                rptID = Request.QueryString["rptID"];
            }


            if (optType == "" || optType == "0") //export report
            {
                if (rptType == "")
                    this.msg.Value=AlertMessage.ExportReportError;
                    //Response.Write(AlertMessage.ExportReportError);
                else
                {
                    string rptName = "";
                    if (rptType.Contains("_Quarterly"))
                    {
                        rptName = rptType.Replace("_Quarterly", " Quarterly");
                    }
                    else if (rptType.Contains("Q"))
                        rptName = rptType.Replace("Q", " Quarterly");
                    else
                        rptName =rptType + " Monthly";

                    if (Session[rptType] == null)
                        this.msg.Value = "TimeOut";
                    else
                    {
                        Response.Clear();
                        Response.Buffer = true;
                        Response.ContentType = "application/vnd.ms-excel";
                        Response.AddHeader("Content-Disposition", "attachment;Filename=" + rptName + ".xls");

                       // Response.Charset = "UTF-8";
                        divReport.InnerHtml = Session[rptType].ToString();
                    }               
                }

            }
            else
            {
                if (rptID == "")
                {
                    this.msg.Value=AlertMessage.DownloadReportError;
                }
                else
                {

                    string rptName = "";
                    DataTable dtReport= ReportHelper.GetHistReportContent(System.Convert.ToInt64(rptID));
                    if (dtReport.Rows.Count > 0)
                    {
                        Response.Clear();
                        Response.Buffer = true;
                        Response.ContentType = "application/vnd.ms-excel"; 
                        rptName = dtReport.Rows[0]["ReportType"].ToString();
                        if (rptName.Contains("_Quarterly"))
                        {
                            rptName = rptName.Replace("_Quarterly", " Quarterly");
                        }
                        else if (rptName.Contains("Q"))
                            rptName = rptName.Replace("Q", " Quarterly");
                        else
                            rptName += " Monthly";

                        rptName += "_" + System.Convert.ToDateTime(dtReport.Rows[0]["ReportDate"].ToString()).ToShortDateString();
                        rptName=rptName.Replace("/", "-");




                        Response.AddHeader("Content-Disposition", "attachment;Filename=" + rptName + ".xls");

                        //Response.Charset = "iso- 8859-1";
                        //Response.Charset = "GB2312";
                        //Response.ContentEncoding = System.Text.Encoding.UTF8;
                        //byte[] bytes = Encoding.ASCII.GetBytes(dtReport.Rows[0]["ReportContent"].ToString());

                        //divReport.InnerHtml = Encoding.UTF8.GetString(bytes);
                        divReport.InnerHtml = dtReport.Rows[0]["ReportContent"].ToString();

                        
                    }
                    else
                    {
                        this.msg.Value=AlertMessage.DownloadReportError;
                    }

                }
            }



            
        }
    }
}
